<?php

/**
 * Wrapper of Zend_Cache Class
 *
 * 
 * @version $Id: Cache.php 1413 2012-02-29 07:10:26Z locth $
 * @author $Author
 *
 */
class SGN_Cache
{
    /**
     *
     * Total of missed cache in a session
     *
     * @var integer
     */
    private $_missCount = 0;
    /**
     *
     * Total of hit cache in a session
     *
     * @var integer
     */
    private $_hitCount = 0;
    /**
     *
     * Main cache object
     *
     * @var Zend_Cache
     */
    private $_cache = null;
    /**
     * 
     * Cache log file
     * 
     * @var string
     */
    private $_logFile = '/home/betasgn/logs/cache.log';
    /**
     * 
     * Configuration options
     * 
     * @var Zend_Config
     */
    public $options = null;

    /**
     *
     * Save Zend_Cache object for later uses
     *
     * @param Zend_Cache $zendCache
     *
     */
    function __construct($zendCache)
    {
        $this->_logFile = ROOT_PATH . '/logs/cache.log';
        $this->_cache = $zendCache;
    }

    /**
     * 
     * Set Configuration Options
     * 
     * @param Zend_Config $options
     * 
     * @return void
     */
    public function setOptions($options)
    {
        $this->options = $options;
    }

    /**
     *
     * Wrapper of Zend_Cache::factory
     *
     * @param string $frontend
     * @param string $backend
     * @param array $frontendOptions
     * @param array $backendOptions
     * @param boolean $customFrontendNaming
     * @param boolean $customBackendNaming
     * @param boolean $autoload
     * @return vPortal_Zend_Cache_Wrapper
     */
    public static function factory($frontend, $backend, $frontendOptions = array(), $backendOptions = array(), $customFrontendNaming = false, $customBackendNaming = false, $autoload = false)
    {
        $zendCache = Zend_Cache::factory($frontend, $backend, $frontendOptions, $backendOptions, $customFrontendNaming, $customBackendNaming, $autoload);
        return new self($zendCache);
    }

    /**
     *
     * Wrapper of Zend_Cache save method
     *
     * @param string $value
     * @param string $key
     * @return boolean
     */
    public function save($value, $key, $tags = array(), $specificLifetime = false)
    {
        if (! $this->_cache) {
            return false;
        }
        if (SGN_Application::isRegistered('profiler')) {
                SGN_Application::get('profiler')->setProfilerData('save', $key . " (" . $this->_calculateCacheSize(serialize($value)) . ")", 'cache');
        }
        $result = $this->_cache->save($value, $key, $tags, $specificLifetime);
        return $result;
    }

    /**
     * Wrapper of Zend_Cache load
     *
     *
     * @param string $key
     * @return string
     */
    public function load($key, $doNotTestCacheValidity = false)
    {
        if (! $this->_cache) {
            return false;
        }
        $originalKey = $key;
        $cacheValue = $this->_cache->load($key);
        if (SGN_Application::isRegistered('profiler')) {
            if (SGN_Application::$params['refreshcache']) {
                return false;
            }
            SGN_Application::get('profiler')->setProfilerData('load', 
            "<a target=\"_blank\" href=\"" . (SGN_Application::isRegistered('siteUrl') ? SGN_Application::get('siteUrl') : '') . "/sgn/test/viewcache?key=$originalKey&product=" . $this->options['product'] . "&instance=" . $this->options['instance'] . "\">$originalKey</a>" . " (<i>" . $this->options['cache_id_prefix'] . "</i>) (" .
             $this->_calculateCacheSize(serialize($cacheValue)) . ") <<a href=\"" . (SGN_Application::isRegistered('siteUrl') ? SGN_Application::get('siteUrl') : '') . "/sgn/test/deletecache?key=$key&product=" . $this->options['product'] . "&instance=" . $this->options['instance'] . "\">delete</a>>", 'cache');
        }
        if ($cacheValue === false) {
            $this->_missCount ++;
            error_log('missed:key:' . $key . "\n", 3, $this->_logFile);
            return false;
        }
        $this->_hitCount ++;
        return $cacheValue;
    }

    private function _calculateCacheSize($cacheValue)
    {
        $cacheSize = strlen($cacheValue);
        $color = 'green';
        if ($cacheSize > 1024) {
            $color = 'red';
        }
        $cacheSize = SGN_Application::bytesFormat($cacheSize);
        return "<span style='color:$color'>" . $cacheSize . "</span>";
    }

    /**
     *
     * Wrapper of Zend_Cache save method
     *
     * @param string $value
     * @param string $key
     * @return boolean
     */
    public function remove($key)
    {
        if (! $this->_cache) {
            return false;
        }
        return $this->_cache->remove($key);
    }

    /**
     *
     * Wrapper of Zend_Cache increment method
     *
     * @param string $value
     * @param string $key
     * @return boolean
     */
    public function increment($key, $offset)
    {
        if (! $this->_cache) {
            return false;
        }
        return $this->_cache->increment($key, $offset);
    }

    /**
     *
     * Wrapper of Zend_Cache test
     *
     * @param string $key
     * @return integer
     */
    public function test($key)
    {
        if (! $this->_cache) {
            return false;
        }
        return $this->_cache->test($key);
    }
}